/* EasyWay Game Engine
 * Copyright (C) 2007 Daniele Paggi.
 *  
 * Written by: 2007 Daniele Paggi<dshnt@hotmail.com>
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.easyway.editor.forms.components.extended;

import java.util.ArrayList;

import org.easyway.editor.commands.Command;
import org.easyway.editor.commands.user.GetTile;
import org.easyway.editor.forms.EditorCore;
import org.easyway.editor.forms.components.Button;
import org.easyway.editor.forms.components.Group;
import org.easyway.editor.forms.components.InputText;
import org.easyway.editor.forms.components.interfaces.IValidity;
import org.easyway.system.StaticRef;
import org.easyway.tiles.Tile;

public class GetTileButton extends Button implements IValidity {

	private static final long serialVersionUID = -8499445364325771438L;

	// ITexture texture;
	Tile tile;

	String name;

	ArrayList<Group> groups;

	public GetTileButton(String label, String name) {
		super(label, "select");
		this.name = name;
		groups = new ArrayList<Group>(10);
		Group.AllObjects.add(this);
		super.label.setIcon(InputText.iconX);
	}

	@Override//
	public void command() {
		if (StaticRef.getCamera().getx() <= -10000+1f)
			return;
		float oldx = StaticRef.getCamera().getx() ;
		float oldy = StaticRef.getCamera().gety() ;
	
		// UEditor.getNavigator().setEnabled(false);
		EditorCore.setCanMoveCamera(false);
		EditorCore.setCanMoveObjects(false);
		new SetCamera(-10000,-10000);
		tile = GetTile.getTile();
		// texture = GetTexture.getTexture();
		boolean value;
		if (value = tile != null) {
			label.setIcon(InputText.iconV);
		}
		for (Group g : groups) {
			g.updateValidity(value);
		}
		new SetCamera(oldx, oldy);
		// UEditor.getNavigator().setEnabled(true);
		EditorCore.setCanMoveCamera(true);
		EditorCore.setCanMoveObjects(true);
	}

	public boolean getValidity() {
		return tile != null;
	}

	public String getName() {
		return name;
	}

	public ArrayList<Group> getGroups() {
		return groups;
	}

	class SetCamera extends Command {

		float x, y;

		public SetCamera(float x, float y) {
			//this.newCamera = newCamera;
			this.x = x;
			this.y = y;
		}

		public void creation() {
			//StaticRef.setCamera(newCamera);
			StaticRef.getCamera().setx(x);
			StaticRef.getCamera().setx(y);
			// label.setIcon(InputText.iconX);
			destroy();

		}

		public void loop() {
		}

		public void render() {
		}

	}
}
